package com.demo.batch.controller;

import com.demo.batch.job.JobFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author
 * @since
 */
@RequestMapping("/job/")
@RestController
@Slf4j
public class TestController {

  @Autowired
  JobLauncher jobLauncher;
  @Autowired
  JobFactory jobFactory;

  @GetMapping("/startEmpty")
  public String startEmptyBatch(){
    Job emptyJob = jobFactory.getJobInstance();
    String result = null;
    Map jobMap = new HashMap<String, String>();
    jobMap.put("say",new JobParameter("hello, batch"));
    JobParameters jobParameters = new JobParameters(jobMap);
    try {
      jobLauncher.run(emptyJob, jobParameters);
      result = "Okay";
    } catch (Exception e){
      log.error("{}", e);
      result = e.getMessage();
    }
    return result;
  }
}


